package com.example.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.example.common.enums.RoleEnum;
import com.example.entity.Account;
import com.example.entity.Collect;
import com.example.entity.Sing;
import com.example.entity.Singer;
import com.example.mapper.CollectMapper;
import com.example.mapper.SingMapper;
import com.example.mapper.SingerMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 歌曲信息表业务处理
 **/
@Service
public class SingService {

    @Resource
    private SingMapper singMapper;
    @Resource
    private CollectMapper collectMapper;
    @Resource
    private SingerMapper singerMapper;

    /**
     * 新增
     */
    public void add(Sing sing) {
        singMapper.insert(sing);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        singMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            singMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(Sing sing) {
        singMapper.updateById(sing);
    }

    /**
     * 根据ID查询
     */
    public Sing selectById(Integer id) {
        return singMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<Sing> selectAll(Sing sing) {
        List<Sing> list = singMapper.selectAll(sing);
        Account currentUser = TokenUtils.getCurrentUser();
        if (ObjectUtil.isNotNull(currentUser)) {
            for (Sing song : list) {
                Collect collect = new Collect();
                collect.setUserId(currentUser.getId());
                collect.setRelId(song.getId());
                collect.setType("SONG");
                List<Collect> collects = collectMapper.selectAll(collect);
                if (CollectionUtil.isNotEmpty(collects)) {
                    song.setCollectId(collects.get(0).getId());
                }
            }
        }
        return list;
    }

    /**
     * 分页查询
     */
    public PageInfo<Sing> selectPage(Sing sing, Integer pageNum, Integer pageSize) {
        Account currentUser = TokenUtils.getCurrentUser();
        if (RoleEnum.USER.name().equals(currentUser.getRole())) {
            sing.setRole(RoleEnum.USER.name());
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Sing> list = singMapper.selectAll(sing);
        if (ObjectUtil.isNotNull(currentUser)) {
            for (Sing song : list) {
                Collect collect = new Collect();
                collect.setUserId(currentUser.getId());
                collect.setRelId(song.getId());
                collect.setType("SONG");
                List<Collect> collects = collectMapper.selectAll(collect);
                if (CollectionUtil.isNotEmpty(collects)) {
                    song.setCollectId(collects.get(0).getId());
                }
            }
        }
        return PageInfo.of(list);
    }

    public List<Sing> selectTop5() {
        List<Sing> list = singMapper.selectTop5();
        for (Sing song : list) {
            Singer singer = singerMapper.selectById(song.getSingerId());
            if (ObjectUtil.isNotEmpty(singer)) {
                song.setSingerName(singer.getName());
                song.setSingerAvatar(singer.getAvatar());
            }
        }
        return list;
    }
}